package manager.cms.service.exec;

import common.SnowflakeIdWorker;
import manager.cms.dao.CmsContentCatDao;
import manager.cms.dao.CmsContentDao;
import manager.cms.service.JdExecutor;
import manager.cms.vo.CmsContentCatVO;
import manager.cms.vo.CmsContentVO;
import manager.feign.ShopIndexFloorThirdService;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 获取首页信息
 */
@Service(value = "topMonetizeExec")
public class TopMonetizeExecImpl implements JdExecutor {
    /**
     * cms分类信息
     */
    @Autowired
    private CmsContentCatDao cmsContentCatDao;
    /**
     * Cms内容信息
     */
    private SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
    @Autowired
    private ShopIndexFloorThirdService shopIndexFloorThirdService;
    @Autowired
    private CmsContentDao cmsContentDao;
    public TopMonetizeExecImpl(){}
    @Override
    public void execute(Map<String,Object> mapParams) {
        try {
            JSONObject jsonObject = null;
            String  topMonetize=shopIndexFloorThirdService.getTopMonetize();
            String jsonStr=topMonetize.substring(28,topMonetize.length()-1);
            jsonObject = new JSONObject(jsonStr);
            JSONObject data=(JSONObject)jsonObject.get("data");
            JSONObject resultData=(JSONObject)(data).get("treasure");
            //获取轮播图信息
            CmsContentCatVO cmsContentCatVO=new CmsContentCatVO();
            cmsContentCatVO.setName("首页导航");
            String id=String.valueOf(idWorker.nextId());
            cmsContentCatVO.setId(id);
            cmsContentCatDao.addCmsContentCat(cmsContentCatVO);
            CmsContentVO cmsContentVO=new CmsContentVO();
            cmsContentVO.setCategoryId(id);
            String contentId=String.valueOf(idWorker.nextId());
            cmsContentVO.setId(contentId);
            cmsContentVO.setPic(resultData.get("src").toString());
            cmsContentVO.setTitle("首页导航图片");
            cmsContentVO.setUrl(resultData.get("href").toString());
            cmsContentVO.setSortOrder(1);
            cmsContentVO.setStatus(true);
            cmsContentDao.addCmsContent(cmsContentVO);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
